package com.education.platform.management.service.impl;

import com.education.platform.common.exception.BusinessException;
import com.education.platform.common.service.impl.BaseServiceImpl;
import com.education.platform.dao.GoodsDao;
import com.education.platform.dao.entity.GoodsDO;
import com.education.platform.management.config.ThreadUser;
import com.education.platform.management.push.PushUtil;
import com.education.platform.management.service.GoodsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
* describe: 商品
* @author : tomato
* @date: 2020-06-29 10:28:07.601
**/
@Service
public class GoodsServiceImpl extends BaseServiceImpl<GoodsDao, GoodsDO> implements GoodsService {

	private static final Logger LOGGER = LoggerFactory.getLogger(GoodsServiceImpl.class);

    @Autowired
	private GoodsDao goodsDao;
    @Autowired
    private PushUtil pushUtil;

    /**
     * 保存
     * @param goodsDO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(GoodsDO goodsDO) {
        if (goodsDO==null) {
            throw new BusinessException("数据为空");
        }
        if(goodsDO.getId()==null){
            //默认不上架
            if(goodsDO.getPublish()==null){
                goodsDO.setPublish(false);
            }
            goodsDO.setCreateBy(ThreadUser.getUserName());
            goodsDO.setCreateTime(new Date());
            goodsDO.setSalesVolume(0);
            goodsDO.setDeleted(false);
            goodsDao.insert(goodsDO);


        }else{
            goodsDO.setUpdateBy(ThreadUser.getUserName());
            goodsDO.setUpdateTime(new Date());
            goodsDao.updateById(goodsDO);
        }
    }
}
